package com.zhuxi.controller;

import com.zhuxi.dao.LagouTokenRepository;
import com.zhuxi.pojo.LagouToken;
import com.zhuxi.service.LagouCodeService;
import com.zhuxi.util.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private LagouTokenRepository lagouTokenRepository;

    @Autowired
    private LagouCodeService lagouCodeService;

    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        Example<LagouToken> example = Example.of(lagouToken);
        List<LagouToken> lagouTokens = lagouTokenRepository.findAll(example);
        if (lagouTokens != null && lagouTokens.size() > 0) {
            return true;//已注册
        } else {
            return false;//未注册
        }
    }

    @GetMapping("/register/{email}/{password}/{code}")
    public Boolean register(@PathVariable String email, @PathVariable String password, @PathVariable String code,
                            HttpServletRequest request, HttpServletResponse response) {
        Integer codeResult = lagouCodeService.validate(email,code);
        if (codeResult != 0) {//验证码校验失败
            return false;
        } else {
            String token = CodeUtils.createToken();
            LagouToken lagouToken = new LagouToken();
            lagouToken.setEmail(email);
            lagouToken.setToken(token);
            lagouTokenRepository.save(lagouToken);
            Cookie cookie = new Cookie("token",token);
            cookie.setPath("/");
            cookie.setMaxAge(60 * 60 * 30);
            response.addCookie(cookie);
            return true;
        }
    }

    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email,@PathVariable String password, HttpServletResponse response) {
        String token = CodeUtils.createToken();
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(token);
        lagouTokenRepository.save(lagouToken);
        Cookie cookie = new Cookie("token",token);
        cookie.setPath("/");
        cookie.setMaxAge(60 * 60 * 30);
        response.addCookie(cookie);
        return email;
    }

    @GetMapping("/info/{token}")
    public String info(@PathVariable String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        Example<LagouToken> example = Example.of(lagouToken);
        List<LagouToken> lagouTokens = lagouTokenRepository.findAll(example);
        return lagouTokens.get(0).getEmail();
    }
}
